What is @use-gesture/react?
@use-gesture/react is a library that provides a set of hooks to handle gestures in React applications. It allows developers to easily add touch and pointer event handling to their components, supporting gestures like dragging, pinching, scrolling, and more.
What are @use-gesture/react's main functionalities?
Drag
This code demonstrates how to use the `useDrag` hook to make a div element draggable. The position of the element is updated based on the drag state.
```jsx
import { useDrag } from '@use-gesture/react';
import { useState } from 'react';
function Draggable() {
const [position, setPosition] = useState({ x: 0, y: 0 });
const bind = useDrag((state) => {
setPosition({ x: state.offset[0], y: state.offset[1] });
});
return (
<div
{...bind()}
style={{
transform: `translate3d(${position.x}px, ${position.y}px, 0)`,
width: 100,
height: 100,
background: 'lightblue',
}}
/>
);
}
```
Pinch
This code demonstrates how to use the `usePinch` hook to make a div element pinchable. The scale of the element is updated based on the pinch state.
```jsx
import { usePinch } from '@use-gesture/react';
import { useState } from 'react';
function Pinchable() {
const [scale, setScale] = useState(1);
const bind = usePinch((state) => {
setScale(state.offset[0]);
});
return (
<div
{...bind()}
style={{
transform: `scale(${scale})`,
width: 100,
height: 100,
background: 'lightcoral',
}}
/>
);
}
```
Scroll
This code demonstrates how to use the `useScroll` hook to handle scroll events. The scroll position is updated based on the scroll state.
```jsx
import { useScroll } from '@use-gesture/react';
import { useState } from 'react';
function Scrollable() {
const [scroll, setScroll] = useState({ x: 0, y: 0 });
const bind = useScroll((state) => {
setScroll({ x: state.offset[0], y: state.offset[1] });
});
return (
<div
{...bind()}
style={{
width: 200,
height: 200,
overflow: 'auto',
background: 'lightgreen',
}}
>
<div style={{ width: 400, height: 400 }}>
Scroll me!
</div>
</div>
);
}
```
Other packages similar to @use-gesture/react
react-use-gesture
react-use-gesture is a library that provides hooks for handling gestures in React applications. It is similar to @use-gesture/react and offers a similar API for handling gestures like dragging, pinching, and scrolling.
react-spring
react-spring is a library for creating animations in React applications. While it is primarily focused on animations, it also provides hooks for handling gestures, making it a versatile choice for developers who need both animations and gesture handling.
react-draggable
react-draggable is a library specifically for making elements draggable in React applications. It provides a simple API for adding drag-and-drop functionality to components, but it does not support other gestures like pinching or scrolling.
@use-gesture
@use-gesture is a library that lets you bind richer mouse and touch events to any component or view. With the data you receive, it becomes trivial to set up gestures, and often takes no more than a few lines of code.
You can use it stand-alone, but to make the most of it you should combine it with an animation library like react-spring, though you can most certainly use any other.
The demos are real click them!
Installation
React
yarn add @use-gesture/react
npm install @use-gesture/react
Vanilla javascript
yarn add @use-gesture/vanilla
npm install @use-gesture/vanilla
Simple example
React
import { useSpring, animated } from '@react-spring/web'
import { useDrag } from '@use-gesture/react'
function Example() {
const [{ x, y }, api] = useSpring(() => ({ x: 0, y: 0 }))
const bind = useDrag(({ down, movement: [mx, my] }) => {
api.start({ x: down ? mx : 0, y: down ? my : 0 })
})
return <animated.div {...bind()} style={{ x, y, touchAction: 'none' }} />
}
Vanilla javascript
<div id="drag" />
const el = document.getElementById('drag')
const gesture = new DragGesture(el, ({ active, movement: [mx, my] }) => {
setActive(active)
anime({
targets: el,
translateX: active ? mx : 0,
translateY: active ? my : 0,
duration: active ? 0 : 1000
})
})
gesture.destroy()
The example above makes a div
draggable so that it follows your mouse on drag, and returns to its initial position on release.
Make sure you always set touchAction
on a draggable element to prevent glitches with the browser native scrolling on touch devices.
Available hooks
@use-gesture/react exports several hooks that can handle different gestures:
Hook | Description |
---|
useDrag | Handles the drag gesture |
useMove | Handles mouse move events |
useHover | Handles mouse enter and mouse leave events |
useScroll | Handles scroll events |
useWheel | Handles wheel events |
usePinch | Handles the pinch gesture |
useGesture | Handles multiple gestures in one hook |